package com.hi5.stexviewer.message.impl;

import static com.hi5.util.Validation.notEmpty;
import static com.hi5.util.Validation.state;

import java.util.Set;

import javax.swing.JFrame;
import javax.swing.SwingUtilities;

import org.springframework.beans.factory.InitializingBean;

import com.hi5.stexviewer.message.CallCenter;
import com.hi5.stexviewer.message.Customer;
import com.hi5.stexviewer.message.Message;

public abstract class JFrameCustomer extends JFrame implements Customer, InitializingBean {
    private static final long serialVersionUID = 7203611737292411051L; 
    private CallCenter callCenter;
    
    public void recieve(final Message<?> message) {
        Set<Object> handledMessageTypes = getHandledMessageTypes();
        if (message.getRecipient() == this || handledMessageTypes == null || handledMessageTypes.contains(message.getType())) {
            SwingUtilities.invokeLater(new Runnable() {
                public void run() {
                    doRecieve(message);
                }
            });
        }
    }

    public void afterPropertiesSet() throws Exception {
        state(notEmpty(callCenter), "CallCenter must be set");
        callCenter.subscribe(this);
    }

    public CallCenter getCallCenter() {
        return callCenter;
    }

    public void setCallCenter(CallCenter callCenter) {
        this.callCenter = callCenter;
    }
    
    protected Set<Object> getHandledMessageTypes() {
        return null;
    }

    protected abstract void doRecieve(Message<?> message);
}
